//
//  AIMessage.swift
//  NDAi
//
//  Created by 丸子 on 2024/6/28.
//

import UIKit
// 0 自己
// 1 思考
// 2 生成
// 3 失败
struct AIMessage: TableObject {
    static var tableName: String = "AIMessage"
    var userId:Int = 0
    var fromUser:Bool = false
    var message:String?
    var state:Int?
    var sort:Int?
    var date:TimeInterval = Date().timeIntervalSince1970
    var dataId:String = "0"
    var collect:Bool = false
    enum CodingKeys: String, CodingTableKey {
        typealias Root = AIMessage
        static let objectRelationalMapping = TableBinding(CodingKeys.self)
        case userId
        case fromUser
        case message
        case state
        case sort
        case date
        case dataId
        case collect
        
        static var columnConstraintBindings: [CodingKeys: ColumnConstraintBinding]? {
            return [
                sort: ColumnConstraintBinding(isPrimary: true, isAutoIncrement: true)
            ]
        }
    }
    
    static func convert(from message: AIStreamMessage) -> AIMessage {
         var m = AIMessage()
         m.fromUser = false
         m.message = message.data
         var t = 0
         switch message.status {
         case .default:
             t = 2
         case .error, .interrupt:
             t = 3
         case .finish:
             t = 0
         }
         m.state = t
         return m
    }
    
//    required init() {
//        
//    }
}
